package com.yc.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClientConfiguration;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author zwei
 * @create 2024-09-02 17:04
 */
@Configuration
@LoadBalancerClients(value = {
        @LoadBalancerClient(value = "res-food",configuration = MyLoadBalancerClientConfiguration.class),
        @LoadBalancerClient(value = "res-security",configuration = MyLoadBalancerClientConfiguration.class),
        @LoadBalancerClient(value = "res-order",configuration = MyLoadBalancerClientConfiguration.class)
},defaultConfiguration = LoadBalancerClientConfiguration.class)
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowedHeaders("*")
                .allowCredentials(true);
    }

    //WEB 的 请求  可以跨域  RestTemplate模板客户端  可以跨域
    @LoadBalanced      // 开启负载均衡  以后ResTemplate发出的请求都会经过负载均衡
    @Bean
    public RestTemplate initRestTemplate() {
        return new RestTemplate();
    }
}